package player.ui.browse;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JComponent;

import net.miginfocom.swing.MigLayout;
import player.Tools;
import player.ui.video.TPanel;

public class TabPanel extends JComponent{
	private static final long serialVersionUID = -7641018557577080193L;
	TabCPanel p1;
	TPanel pc;
	TPanel p2;
	boolean show=true;
	JComponent select;
	HashMap<String, JComponent> cTable=new HashMap<String, JComponent>();
	TabPanel(){
		super();
		setLayout(new MigLayout("insets 0 0 0 0 ","","[]0[]0[]"));

		
		p2=new TPanel();
		p2.setLayout(new TLayout());
		add(p2,"width :2048: ,height 0:2048: ,wrap ,span");
		
		p1=new TabCPanel(this);
		p1.setLayout(new MigLayout("insets 0 0 0 0,align right","[]","[]"));
		//add(p1,"width 0:2048: ,height :: ");
		
		pc=new TPanel();
		pc.setLayout(new MigLayout("insets 0 0 0 0"));
		//p1.add(pc);
		//add(pc,"width :200: ,height :: ,wrap");
		
	}
	
	
	void addTab(final JComponent c){
		cTable.put(c.getName(), c);
		JButton b=Tools.creatButton(c.getName());
		p1.add(b);
		p1.setComponentZOrder(b, 0);
		p2.add(c);
		c.setVisible(false);
		b.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				if(isShowTab(c)){
					hide();
				}else{
					showTab(c);
				}
			}
			
		});
	}
	
	boolean isShowTab(JComponent c){
		return c.isVisible();
	}
	
	void showTab(JComponent c1){
		show=true;
		select=c1;
		Component[] cs=p2.getComponents();
		for(Component c2:cs){
			if(c2.equals(c1)){
				c2.setVisible(true);
			}else{
				c2.setVisible(false);
			}
		}
		p1.setVisible(true);
	}
	
	public boolean isShow(){
		return show;
	}
	
	public void show(boolean b){
		show=b;
		if(show){
			showTab(select);
		}else{
			hide();
		}
	}
	
	public void hide(){
		show=false;
		Component[] cs=p2.getComponents();
		for(Component c2:cs){
			c2.setVisible(false);
		}
	}
	
	void addControlButton(JComponent c1){
		pc.add(c1);
	}
	
}


class TabCPanel extends TPanel{
	TabPanel TabPanel;
	TabCPanel(TabPanel TabPanel){
		super();
		this.TabPanel=TabPanel;
	}
	
	public void setVisible(boolean b){
		if(b){
			super.setVisible(true);
		}else{
			if(!TabPanel.isShow()){
				super.setVisible(false);
			}
		}
	}
}

class TLayout implements LayoutManager{

	public void addLayoutComponent(String name, Component comp) {
		
	}

	public void layoutContainer(Container cn) {
		int count = cn.getComponentCount();
		  for (int i = 0 ; i < count ; i++) {
		    	Component m = cn.getComponent(i);
		    	Rectangle rec=new Rectangle();
	    		rec.x=0;
	    		rec.y=0;
	    		rec.width=cn.getWidth();
	    		rec.height=cn.getHeight();
	    		m.setBounds(rec);
		  }
	}

	public Dimension minimumLayoutSize(Container parent) {
		return new Dimension(0,0);
	}

	public Dimension preferredLayoutSize(Container parent) {
		return new Dimension(0,0);
	}

	public void removeLayoutComponent(Component comp) {
		
	}

}